package com.xiaoyu.lanling.feature.fate.data

import `in`.srain.cube.request.FailData
import `in`.srain.cube.request.JsonData
import `in`.srain.cube.request.RequestHandler
import `in`.srain.cube.views.list.PagedListDataModelWithRequestTag
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.net.RequestCreator
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.common.config.HttpConfig
import com.xiaoyu.lanling.event.fate.MainFateUserListEvent
import com.xiaoyu.lanling.feature.fate.model.MainFateUserItem
import com.xiaoyu.lanling.feature.user.model.UserExtra

class MainFateUserListData<ItemType, RequestType> : PagedListDataModelWithRequestTag<MainFateUserItem, Any>() {

    private val requestTag = Any()
    private val unlimitedDoc = AppContext.getString(R.string.fate_preference_location_unlimited)

    override fun doQueryData() {
        RequestCreator.create<MainFateUserListEvent>(MainFateUserListEvent::class.java)
                .setRequestUrl(HttpConfig.LANLING_PROMOTION_LIST)
                .addQueryData("verify", UserExtra.instance.preference.verify)
                .addQueryData("sex", UserExtra.instance.preference.sex)
                .addQueryData("province", if (UserExtra.instance.preference.province == unlimitedDoc) "" else UserExtra.instance.preference.province)
                .addQueryData("city", if (UserExtra.instance.preference.city == unlimitedDoc) "" else UserExtra.instance.preference.city)
                .addQueryData("minAge", UserExtra.instance.preference.minAge)
                .addQueryData("maxAge", UserExtra.instance.preference.maxAge)
                .appendQueryDataTo(listPageInfo)
                .setRequestHandler(object : RequestHandler<MainFateUserListEvent, JsonData> {
                    override fun processOriginData(originData: JsonData): MainFateUserListEvent {
                        val rawData = originData.optJson("data")
                        val list = mutableListOf<MainFateUserItem>()

                        val rawList = rawData.optJson("list").toList()
                        val isFirstPage = listPageInfo.isFirstPage
                        val preLen = if (isFirstPage) 0 else listPageInfo.dataList.size
                        for ((i, item) in rawList.withIndex()) {
                            list.add(MainFateUserItem(i + preLen, item))
                        }
                        updateResult(rawData, list)
                        return MainFateUserListEvent(requestTag, isFirstPage)
                    }

                    override fun onRequestFail(failData: FailData) {
                        unlockOnRequestFail()
                        MainFateUserListEvent(requestTag, true).fail().post()
                    }

                    override fun onRequestFinish(event: MainFateUserListEvent) {
                        event.post()
                    }
                })
                .enqueue()
    }

    override fun getRequestTag(): Any {
        return requestTag
    }
}
